package br.com.k19.jpa;

import java.util.List;
import java.util.Scanner;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class K12Hibernate {
	
	public static EntityManagerFactory getEntityManagerFactory(){
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("livraria_pu");
		return factory;
	}
	
	public static void setInsereEditora(){
		EntityManagerFactory factory = getEntityManagerFactory();
		EntityManager manager = factory.createEntityManager();
		
		Editora e = new Editora();
		
		Scanner Entrada = new Scanner(System.in);
		System.out.println("Informe o Nome da Editora");
		e.setNome(Entrada.nextLine());
		
		System.out.println("Informe o Email da Editora");
		e.setEmail(Entrada.nextLine());
		
		manager.persist(e);
		
		manager.getTransaction().begin();
		manager.getTransaction().commit();
		
		
		manager.close();
		factory.close();
		Entrada.close();
	}
	
	public static List<Editora> getListaEditora(){
		EntityManagerFactory factory = getEntityManagerFactory();
		EntityManager manager = factory.createEntityManager();
		
		
		String sql = "SELECT e FROM Editora e";
		Query query = manager.createQuery(sql);
		List<Editora> xRetun = query.getResultList();
		
		return xRetun;
	}
	
}
